package com.cskaoyan.controller;

import com.cskaoyan.bean.promotion.GrouponRules;
import com.cskaoyan.service.promotion.GrouponService;
import com.cskaoyan.vo.BaseRespVo;
import com.cskaoyan.vo.promotion.GrouponListRecordVo;
import com.cskaoyan.vo.promotion.GrouponListVo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @description:
 * @autyor:caicaicai
 */
@RestController
public class GrouponController {

    @Autowired
    GrouponService grouponService;

    //?page=1&limit=20&goodsId=8&sort=add_time&order=desc
    @RequestMapping("/admin/groupon/list")
    @RequiresPermissions("admin:groupon:list")
    public BaseRespVo list(Integer page, Integer limit, String sort, String order, Integer goodsId){
        GrouponListVo grouponListVo = grouponService.list(page, limit, sort, order, goodsId);
        return BaseRespVo.ok(grouponListVo);
    }

    @RequestMapping("/admin/groupon/create")
    @RequiresPermissions("admin:groupon:create")
    public BaseRespVo create(@RequestBody GrouponRules grouponRules){
        GrouponRules grouponRulesVo = grouponService.create(grouponRules);
        if(grouponRulesVo == null){
            //return BaseRespVo.fail("参数不存在");
            return BaseRespVo.error(402,"参数不存在");
        }
        return BaseRespVo.ok(grouponRulesVo);
    }

    @RequestMapping("/admin/groupon/update")
    @RequiresPermissions("admin:groupon:update")
    public BaseRespVo update(@RequestBody GrouponRules grouponRules) {
        int code = grouponService.update(grouponRules);
        if (code == 404) {
            //return BaseRespVo.fail("编辑失败！");
            return BaseRespVo.error(402,"编辑失败");
        }else if (code == 10000){
            //return BaseRespVo.fail("参数值不对");
            return BaseRespVo.error(402,"参数不存在");
        }
        return BaseRespVo.ok(grouponRules);
    }

    @RequestMapping("/admin/groupon/delete")
    @RequiresPermissions("admin:groupon:delete")
    public BaseRespVo delete(@RequestBody GrouponRules grouponRules){
        int code = grouponService.delete(grouponRules);
        if (code != 200) {
            //return BaseRespVo.fail("删除失败！");
            return BaseRespVo.error(402,"删除失败");
        }
        return BaseRespVo.ok();
    }

    @RequestMapping("/admin/groupon/listRecord")
    @RequiresPermissions("admin:groupon:read")
    public BaseRespVo listRecord(Integer page, Integer limit, String sort, String order){
        GrouponListRecordVo grouponListRecordVo = grouponService.listRecord(page, limit, sort, order);
        return BaseRespVo.ok(new GrouponListRecordVo());
    }

}
